/*
 * Copyright 2010, 2011, 2012 mapsforge.org
 *
 * This program is free software: you can redistribute it and/or modify it under the
 * terms of the GNU Lesser General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */
package org.mapsforge.applications.android.samples;

import java.io.File;
import java.io.FileNotFoundException;

import org.mapsforge.android.maps.MapActivity;
import org.mapsforge.android.maps.MapView;
import org.mapsforge.android.maps.mapgenerator.tiledownloader.MapnikTileDownloader;
import org.mapsforge.android.maps.overlay.KMLOverlay;
import org.mapsforge.applications.android.samples.filepicker.FilePicker;

import android.content.Intent;
import android.os.Bundle;

/**
 * An application which demonstrates how to use different types of overlays.
 */
public class CustomOverlayMapViewer extends MapActivity {

	private static final int SELECT_MAP_FILE = 0;

	MapView mapView;
	File f;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		this.mapView = new MapView(this, new MapnikTileDownloader());
		this.mapView.setClickable(true);
		this.mapView.setBuiltInZoomControls(true);
		setContentView(this.mapView);
	}

	@Override
	protected void onPostCreate(Bundle savedInstanceState) {
		super.onPostCreate(savedInstanceState);

		if (this.f == null)
			startMapFilePicker();
	}

	@Override
	public void onSaveInstanceState(Bundle savedInstanceState) {
		super.onSaveInstanceState(savedInstanceState);
		savedInstanceState.putSerializable("FileName", this.f);
	}

	@Override
	public void onRestoreInstanceState(Bundle savedInstanceState) {
		super.onRestoreInstanceState(savedInstanceState);
		this.f = (File) savedInstanceState.getSerializable("FileName");
	}

	/**
	 * Starts the FilePicker to select a file.
	 */
	private void startMapFilePicker() {
		startActivityForResult(new Intent(this, FilePicker.class), SELECT_MAP_FILE);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
		if (requestCode == SELECT_MAP_FILE) {
			if (resultCode == RESULT_OK) {
				if (intent != null && intent.getStringExtra(FilePicker.SELECTED_FILE) != null && this.mapView != null) {
					this.f = new File(intent.getStringExtra(FilePicker.SELECTED_FILE));
					UpdateKMLOverlay();
				}
			} else if (resultCode == RESULT_CANCELED) {
				finish();
			}
		}
	}

	private void UpdateKMLOverlay() {

		// File f = new File(Environment.getExternalStorageDirectory().getPath(), "university.kml"); //
		// ("/home/art/Documents/PFC2/UniVersity.kml");

		if (this.f != null) {

			KMLOverlay k = new KMLOverlay();
			// TODO: find a better way to do this.. is better to force a constructor with drawable parameter?
			k.setDefaultItemMarker(this.getResources().getDrawable(R.drawable.marker_red));

			try {
				k.setKML(this.f);
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				System.err
						.println("What the bloody hell are you doing You piece of filthy shit!?? the file you want to parse does not even exist!");
			}

			// add all overlays to the MapView
			this.mapView.getOverlays().addAll(k);
		}
	}
}
